package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.LayoutDialogPayingBinding;

public class PayDialog extends Dialog {

    public ObservableField<String> result = new ObservableField<>();
    private final LayoutDialogPayingBinding binding;

    public PayDialog(@NonNull Context context) {
        super(context, R.style.myProgressDialogStyle);
        setCanceledOnTouchOutside(false);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_paying,
                null,
                false);
        binding.setUtils(this);
        setContentView(binding.getRoot());
    }

    public void success(){
        result.set("下单成功");
        binding.pbStatus.setVisibility(View.GONE);
        binding.ivStatus.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        },2000);
    }

    public void failed(){
        result.set("下单失败，请重试");
        binding.pbStatus.setVisibility(View.GONE);
        binding.ivStatus.setImageResource(R.mipmap.ic_error);
        binding.ivStatus.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        },2000);
    }

    public void ordering(){
        result.set("下单中...");
    }

    public void paying(){
        result.set("支付中...");
    }

    public void payError(){
        result.set("支付失败");
        binding.pbStatus.setVisibility(View.GONE);
        binding.ivStatus.setImageResource(R.mipmap.ic_error);
        binding.ivStatus.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        },2000);
    }

    public void paySuccess(){
        result.set("支付成功");
        binding.pbStatus.setVisibility(View.GONE);
        binding.ivStatus.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        },2000);
    }
}
